using System;
using System.Collections.Generic;
using System.Data;

using Elev.Core.DAL;

namespace Elev.Core
{
    public class Student : IDimension
    {
        int id;
        string firstName;
        string lastName;
        string note;

        public int ID { get { return id; } }

        public string FirstName
        {
            get { return firstName; }
            set { firstName = ensurePascalCasing(value); }
        }

        public string LastName
        {
            get { return lastName; }
            set { lastName = ensurePascalCasing(value); }
        }

        public string Note
        {
            get { return note; }
            set { note = value; }
        }

        public Student(DataRow row)
        {
            parse(row);
        }
        
        public Student() : this(-1, "", "", "") { }

        public Student(int ID, string FirstName, string LastName, string Note)
        {
            id = ID;
            firstName = FirstName;
            lastName = LastName;
            note = Note;
        }

        void parse(DataRow dr)
        {
            id = Convert.ToInt32(dr["ID"]);
            firstName = (string)dr["FirstName"];
            lastName = (string)dr["LastName"];
            note = (string)dr["Note"];
        }

        string ensurePascalCasing(string input)
        {
            if (input.Length > 0)
            {
                input = input.Trim().ToLower();
                return input.Substring(0, 1).ToUpper() + input.Substring(1);
            }
            return input;
        }

        #region IDimension Members

        public int Save()
        {
            StudentInternal studentInternal = new StudentInternal();
            if (id == -1)
            {
                // New creation ...
                id = studentInternal.Add(firstName, lastName, note);
            }
            else
            {
                studentInternal.Update(id, firstName, lastName, note);
            }
            return id;
        }

        public void Delete()
        {
            StudentInternal studentInternal = new StudentInternal();
            studentInternal.Delete(id);
        }

        #endregion        

        public override string ToString()
        {
            return string.Format("{0}, {1}", lastName, firstName);
        }

        public string ToToolTipString()
        {
            return note;
        }
    }
}
